from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select

wd = webdriver.Chrome()
wd.get("http://49.233.108.117:3000/signin")

# 登录
wd.find_element_by_id("name").send_keys("test1")
wd.find_element_by_id("pass").send_keys("123456")
wd.find_element_by_xpath('//input[@value = "登录"]').click()

js = "window.open('http://49.233.108.117:3000/topic/create')"
wd.execute_script(js)

windows = wd.window_handles
wd.switch_to.window(windows[-1])

# 发布话题
# wd.find_element_by_xpath('//a/span[text() = "发布话题"]').click()
ele = wd.find_element_by_id("tab-value")
Select(ele).select_by_value("share")
wd.find_element_by_id("title").send_keys("测试自动发帖啊")
div = wd.find_element_by_css_selector('.CodeMirror-scroll')
div.click()
# 键盘操作   输入加粗的文字  ctrl+b加粗   shift+home选中
ActionChains(wd).move_to_element(div).send_keys("12345").pause(3).\
    send_keys(Keys.ENTER).\
    send_keys("第二行文字").\
    key_down(Keys.SHIFT).key_down(Keys.HOME).pause(3).key_up(Keys.HOME).key_up(Keys.SHIFT).pause(1)\
    .send_keys(Keys.CONTROL,'b')\
    .perform()
# wd.find_element_by_css_selector('input[value="提交"]').click()

